package cn.edu.scau.cmi.heyonglin.client;

import java.util.Scanner;

import cn.edu.scau.cmi.heyonglin.consistentComposite.ConsistentLeaf;
import cn.edu.scau.cmi.heyonglin.safeComposite.SafeComponent;
import cn.edu.scau.cmi.heyonglin.safeComposite.SafeComposite;
import cn.edu.scau.cmi.heyonglin.safeComposite.SafeLeaf;



public class SafeConpositeClient {
	public static void main(String[] args) {
		SafeComponent []data=new SafeComponent[50];
		int team=0;
		Scanner sc=new Scanner(System.in);
		while(true) {
		int s=0;
		System.out.println("请输入两个字符串");
		String str1=sc.nextLine();
		String str2=sc.nextLine();
		System.out.print("请输入：");
		String object=sc.nextLine();
		for(int i=0;i<team;i++) {
			if(object.equals(data[i].getName())) {
				SafeComponent firstBranch=new SafeLeaf();
				((SafeLeaf)firstBranch).setName(str1);
				SafeComponent secondBranch=new SafeLeaf();
				((SafeLeaf)secondBranch).setName(str2);
				((SafeComposite)data[i]).addPerson(firstBranch);
				((SafeComposite)data[i]).addPerson(secondBranch);
				s=1;
				break;
			}	
		}
		if(s==0) {
			team++;
			SafeComponent trunk=new SafeComposite();
			((SafeComposite)trunk).setName(object);
			SafeComponent firstBranch=new SafeLeaf();
			((SafeLeaf)firstBranch).setName(str1);
			SafeComponent secondBranch=new SafeLeaf();
			((SafeLeaf)secondBranch).setName(str2);
			((SafeComposite)trunk).addPerson(firstBranch);
			((SafeComposite)trunk).addPerson(secondBranch);
			data[team-1]=trunk;
		}
		for(int i=0;i<=team-1;i++) reading(data[i]);
		}
		
	}
     public	static void reading(SafeComponent t) {

		if (t.getPerson()!= null) {
			System.out.println("名字  " + t.getName());
			for (SafeComponent child : t.getPerson()) {
				reading(child);
			}
			System.out.println();
		} else {
			System.out.println("名字" + t.getName());
		}
     }
}
